package com.logic.landseaserver.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.exception.BusinessException;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.persistence.read.ActivityReadMapper;
import com.logic.landseaserver.service.IPubActivityService;
import com.logic.landseaserver.ws.dto.ActivityDTO;
import com.logic.landseaserver.ws.dto.PubActivityDTO;
import com.logic.system.domain.SystemFile;
import com.logic.system.service.impl.FileService;


/**
 * @Author: jack
 * @Date: 2017/5/4
 * @Description: 活动实现类（网站，手机，微信接口）
 */
@Service
public class PubActivityServiceImpl implements IPubActivityService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
	@Autowired
	private ActivityReadMapper activityReadMapper;
	
    @Autowired
    private FileService fileService;
	
	@Override
	public List<PubActivityDTO> getActivityListForFlag(HashMap map) throws BusinessException{
		List<PubActivityDTO> pubRes =new ArrayList<>();
		HashMap hm = new HashMap();
		String flag = map.get("flag").toString();
		String [] s1 = flag.split(",");
		List ar = new ArrayList();
		for(int i=0;i<s1.length;i++){
			ar.add(s1[i]);
		}
		hm.put("flag", ar);
		if(null != map.get("area") && !("".equals(map.get("area")))){
//		if(!StringUtils.isEmpty(map.get("area").toString())){
			hm.put("area", map.get("area").toString());
		}
		
		
		List<ActivityDTO> result = activityReadMapper.getActivityForFlag(hm);
		for(int i=0;i<result.size();i++)
		{
			PubActivityDTO pdto = new PubActivityDTO();
			ActivityDTO act = result.get(i);
			pdto.setId(act.getId());
			pdto.setUserId(act.getUserId());
			pdto.setProId(act.getProId());
			pdto.setSerial(act.getSerial());
			String title = null == act.getTitle() ? "" : act.getTitle();
			pdto.setTitle(title);
			String description = null == act.getDescription() ? "" : act.getDescription();
			pdto.setDescription(description);
			pdto.setImage(act.getImage());
			pdto.setFlag(act.getFlag());
			pdto.setCreatedDtm(act.getCreatedDtm());
			pdto.setCreatedBy(act.getCreatedBy());
			pdto.setLastModifiedDtm(act.getLastModifiedDtm());
			pdto.setLastModifiedBy(act.getLastModifiedBy());
			pdto.setVersion(act.getVersion());
			pdto.setDeleted(act.getDeleted());
			
			//截图下滑线
			if(!StringUtils.isEmpty(act.getCodedesczh())){
				String strCode =act.getCodedesczh();
				String[] sp = strCode.split("_");
				String resultStr = "";
				if(sp.length > 1){
					resultStr = sp[1];
				}else
				{
					resultStr = sp[0];
				}
				pdto.setCodedesczh(resultStr);
			}else{
				pdto.setCodedesczh(act.getCodedesczh());
			}
//			pdto.setCodedesczh(act.getCodedesczh());
			
		    List<SystemFile> detailImgs = new ArrayList<>();
		    if(null != act.getImage() && !"".equals(act.getImage()))
//		    if(!StringUtils.isEmpty(act.getImage()))
		    {
			    try {
					for(String imgid : act.getImage().split(",")){
			            SystemFile  img = (SystemFile)fileService.get(imgid);
						detailImgs.add(img);
			        }
					pdto.setImgs(detailImgs);
				    pubRes.add(pdto);
			    } catch (BusinessException e) {
			        logger.error("getActivityListForFlag error.", e);
				}
		    }
		}
        
		return pubRes;
	}


	@Override
	public void delete(String arg0) throws BusinessException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public Object get(Integer arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public int insert(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public void update(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public Boolean validate(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Boolean validateForDelete(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Boolean validateForInsert(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public Boolean validateForUpdate(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<?> query(QueryUtil arg0) {
		// TODO Auto-generated method stub
		return null;
	}


	

}
